<?php

if(!defined('STARGB_PATH')) {
    define('STARGB_PATH', MODX_BASE_PATH . 'assets/modules/stargb/');
}
if(!defined('STARGB_URL')) {
    define('STARGB_URL', MODX_BASE_URL . 'assets/modules/stargb/');
}

// language
require_once STARGB_PATH . 'StarLng.php';
if (file_exists(STARGB_PATH . 'lng/'.$modx->config['manager_language'].'.ini')) {
    StarLng::load(STARGB_PATH . 'lng/'.$modx->config['manager_language'].'.ini');
}

// variabled
$set = array();
$set['tpl'] = $tpl = 'stargb_tpl';
$set['msg'] = $msg = 'stargb_msg';
$set['form'] = $msg = 'stargb_form';

// StarGB
require_once STARGB_PATH . 'StarGB.php';

$gb = new StarGB($modx, $GLOBALS);

if ((!empty($_POST['message']))and(!empty($_POST['author']))) {
    $gb->addMessage($_POST['author'], $_POST['email'], $_POST['message'], $_POST['reply']);
}

$sql = "SELECT * FROM `{$gb->prefix}star_gb` WHERE ((approved = 1) OR (ip = '{$_SERVER['REMOTE_ADDR']}')) ORDER BY id DESC";
$data = $gb->fetchAll($sql);

$content = '';
$odd = false;
foreach ($data as $row) {
    $msgdata = array(
        'id' => htmlspecialchars($row['id']),
        'date' => date("G:i j. n. Y", strtotime($row['date'])),
        'author' => htmlspecialchars($row['author']),
        'message' => nl2br(htmlspecialchars($row['message'])),
        'ip' => htmlspecialchars(preg_replace("/[0-9]+$/", '*', $row['ip'])),
        'odd' => (!$odd) ? 'even' : 'odd'
    );
    $content .= $modx->parseChunk($set['msg'], $msgdata, '[+', '+]');
    $odd = !$odd;
}

$modx->setPlaceholder('form', $modx->getChunk($set['form']));
$modx->setPlaceholder('content', $content);

// Return the chunk
echo $modx->getChunk($set['tpl']);
//$gb->dispatch('snippet');
//echo 'StarGB';
